package com.windea.study.kotlin.game.tankwar2.core

import javafx.scene.canvas.*
import javafx.scene.image.*
import javafx.scene.paint.*
import javafx.scene.text.*

object Painter {
	private lateinit var graphicsContext: GraphicsContext
	private val imageCache = mutableMapOf<String, Image>()
	
	internal fun set(graphicsContext: GraphicsContext) {
		this.graphicsContext = graphicsContext
	}
	
	/**
	 * 获取图片大小
	 */
	fun size(imagePath: String): Pair<Int, Int> {
		val image = imageCache.getOrPut(imagePath) { Image(imagePath) }
		return image.width.toInt() to image.height.toInt()
	}
	
	/**
	 * 绘制图片
	 */
	fun drawImage(imagePath: String, x: Int, y: Int) {
		val image = imageCache.getOrPut(imagePath) { Image(imagePath) }
		graphicsContext.drawImage(image, x.toDouble(), y.toDouble())
	}
	
	/**
	 * 绘制颜色
	 */
	fun drawColor(color: Color, x: Int, y: Int, width: Int, height: Int) {
		graphicsContext.fill = color
		graphicsContext.fillRect(x.toDouble(), y.toDouble(), width.toDouble(), height.toDouble())
		graphicsContext.fill = Color.BLACK
	}
	
	/**
	 * 绘制文本
	 */
	fun drawText(text: String, x: Int, y: Int, color: Color = Color.BLACK, font: Font = Font.getDefault()) {
		graphicsContext.fill = color
		graphicsContext.font = font
		graphicsContext.fillText(text, x.toDouble(), y.toDouble())
		graphicsContext.font = Font.getDefault()
		graphicsContext.fill = Color.BLACK
	}
}
